<?php


namespace seecms\lib\builder\control;

class DateControl extends Control
{
    protected $type = 'date';

    protected $format = 'date';

    /**
     * @var bool
     */
    protected $range = false;

    /**
     * @param $name
     * @return DateControl|static
     */
    public static function init($name = null)
    {
        $self = new static();
        $self->name = $name;
        return $self;
    }

    /**
     * @return string
     */
    public function getFormat(): string
    {
        return $this->format;
    }

    /**
     * @param string $format
     * @return DateControl
     */
    public function setFormat(string $format)
    {
        $this->format = $format;

        return $this;
    }

    /**
     * @return bool
     */
    public function isRange(): bool
    {
        return $this->range;
    }

    /**
     * @param bool $range
     * @return DateControl
     */
    public function setRange(bool $range = true)
    {
        $this->range = $range;

        return $this;
    }

    public function render(): string
    {

        $required = $this->required ? '<span class="layui-font-red">*</span>' : '';

        $readonly = $this->readonly ? 'readonly' : '';
        $disabled = $this->disabled ? 'disabled' : '';
        $description = $this->getDescription();

        $display = 'block';
        $container = '<div class="layui-form-item">';
        if ($this->inline == 'inline') {
            $display = 'inline';
            $container = '<div class="layui-inline">';
        }


        return <<<EOF
$container
    <label class="layui-form-label">
       {$this->label}
       {$required}
    </label>
    <div class="layui-input-$display">
        <input type="text"
               class="layui-input layui-input"
               name="{$this->name}"
               placeholder="{$this->placeholder}"
               value="{$this->value}"
               id="control-id-{$this->name}"
               data-date
               data-range="{$this->range}"
               data-format="{$this->format}"
               $readonly
               $disabled
        >
        $description
    </div>
</div>
EOF;

    }
}